#include "drv_timer.h"

/**************************************
函数名：drv_timer6_init
函数功能：定时器6初始化
输入参数：无
输出参数：无
其它： 定时500ms中断一次
		分频=8400；装载值=5000；
	  
****************************************/
void drv_timer6_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);//开启TIM6时钟
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割，输入才用
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//选择计数方式为向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 5000-1;//装载值(就是数多少次)
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;//预分频值
	TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStruct);//用上面配置的参数初始化定时器6
	TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);//指定中断源为定时器6的更新中断

	NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;//中断通道号
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断
	NVIC_Init(&NVIC_InitStruct);
	TIM_Cmd(TIM6, ENABLE);
}

/**************************************
函数名：drv_timer13_init
函数功能：定时器13初始化
输入参数：无
输出参数：无
其它： 定时5ms中断一次
		分频=84；装载值=5000；
****************************************/
void drv_timer13_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//开启GPIOF时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);//开启TIM13时钟

	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;//引脚为8
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
	GPIO_Init(GPIOF,&GPIO_InitStruct);
	GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_TIM13);
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割，输入才用
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//选择计数方式为向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 5000-1;//装载值(就是数多少次)
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;//预分频值
	TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStruct);//用上面配置的参数初始化定时器6

	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//输出模式为PWM1
	TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCPolarity_Low;//输出型极低
	TIM_OCInitStruct.TIM_OutputState = ENABLE;//使能输出
	TIM_OCInitStruct.TIM_Pulse = 0;//比较初值
	TIM_OC1Init(TIM13, &TIM_OCInitStruct);
	
	TIM_ARRPreloadConfig(TIM13, ENABLE);//开启装载的影子寄存器
	TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);//开启的比较的影子寄存器
	TIM_CtrlPWMOutputs(TIM13,ENABLE);//使能输出PWM输出
	TIM_Cmd(TIM13, ENABLE);//使能定时器 13
}

/**************************************
函数名：drv_timer3_init
函数功能：定时器3初始化
输入参数：无
输出参数：无
其它： 定时500ms
		分频=84；装载值=5000；
****************************************/
void drv_timer3_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//开定时器3的时钟
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割，输入才用
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 5000-1;//装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;//预分频值
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);//用上面配置的参数初始化定时器6
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//指定中断源为定时器6的更新中断
	TIM_Cmd(TIM3, ENABLE);//使能定时器3
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;//中断通道号
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断
	NVIC_Init(&NVIC_InitStruct);
	
}

/**************************************
函数名：drv_timer4_init
函数功能：定时器4初始化
输入参数：无
输出参数：无
其它： 定时500ms
		分频=84；装载值=5000；
****************************************/
void drv_timer4_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//开定时器3的时钟
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割，输入才用
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 5000-1;//装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;//预分频值
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);//用上面配置的参数初始化定时器6
	TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//指定中断源为定时器6的更新中断
	TIM_Cmd(TIM4, ENABLE);//使能定时器3
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM4_IRQn;//中断通道号
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断
	NVIC_Init(&NVIC_InitStruct);
	
}



